package com.deer.wms.ware.task.web;

import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.ware.task.model.Box.BoxDetail;
import com.deer.wms.ware.task.model.Box.BoxDetailCriteria;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.pickTask.PickTask;
import com.deer.wms.ware.task.service.BoxDetailService;
import com.deer.wms.ware.task.service.PickTaskService;
import com.deer.wms.ware.task.service.SoDetailService;
import com.deer.wms.ware.task.service.SoMasterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Modified by wei on 2021/10/12.
 */
@RestController
@RequestMapping("/box/details")
public class BoxDetailController {

    @Autowired
    private BoxDetailService boxDetailService;

    @Autowired
    private PickTaskService pickTaskService;

    @Autowired
    private SoDetailService soDetailService;

    @Autowired
    private SoMasterService soMasterService;

    /**
     * 装箱复核
     *
     * @param boxDetail boxDetail
     * @return Result
     */
    @PostMapping("/insert")
    public Result add(@RequestBody BoxDetail boxDetail) {

        String time = DateUtils.getNowDateTimeString();

        //Step1.保存boxDetail
        SoMaster soMaster = soMasterService.getById(boxDetail.getSoMasterId());
        boxDetail.setBillNo(soMaster.getBillNo());
        boxDetailService.save(boxDetail);

        //Step2.更新对应的拣货任务的复核数量ReviewQuantity
        PickTask pickTask = pickTaskService.getById(boxDetail.getPickTaskId());
        pickTask.setReviewQuantity(pickTask.getReviewQuantity() + boxDetail.getQuantity());
        pickTaskService.updateById(pickTask);

        //Step3.
//        SoDetail soDetail = soDetailService.getById(boxDetail.getSoDetailId());
//        soDetail.setDeliveryQuantity(soDetail.getDeliveryQuantity()+boxDetail.getQuantity());
//        soDetailService.updateById(soDetail);

//        soMaster.setActualDeliveryTime(time);
//        soMasterService.updateById(soMaster);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 装箱撤回
     *
     * @param boxDetailId boxDetailId
     * @return Result
     */
    @GetMapping("/withdraw")
    public Result withdraw(Integer boxDetailId) {

        BoxDetail boxDetail = boxDetailService.getById(boxDetailId);

        //Step1.更新对应的拣货任务的复核数量ReviewQuantity
        PickTask pickTask = pickTaskService.getById(boxDetail.getPickTaskId());
        pickTask.setReviewQuantity(pickTask.getReviewQuantity() - boxDetail.getQuantity());
        pickTaskService.updateById(pickTask);

        //Step2.
//        SoDetail soDetail = soDetailService.getById(boxDetail.getSoDetailId());
//        soDetail.setDeliveryQuantity(soDetail.getDeliveryQuantity()-boxDetail.getQuantity());
//        soDetailService.updateById(soDetail);
//
//        SoMaster soMaster = soMasterService.findBy("bill_no",soDetail.getBillNo());
//        soMaster.setActualDeliveryTime(null);
//        soMasterService.updateById(soMaster);

        boxDetailService.removeById(boxDetailId);

        return ResultGenerator.genSuccessResult();
    }


    @GetMapping("/delete")
    public Result delete(Integer id) {
        boxDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody BoxDetail boxDetail) {
        boxDetailService.updateById(boxDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@PathVariable Integer id) {
        BoxDetail boxDetail = boxDetailService.getById(id);
        return ResultGenerator.genSuccessResult(boxDetail);
    }

    @GetMapping("/list")
    public Result list(BoxDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<BoxDetail> list = boxDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
